package com.ojoin.trade.supplyloan_core.web.messagequeue;

import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.ObjectMessage;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.ojoin.trade.bpm.web.service.BpmTaskService;
import com.ojoin.trade.bpm.web.support.ProcessMessageVO;
import com.ojoin.trade.common.exception.BusinessException;
import com.ojoin.trade.common.utils.LogUtils;

/**
 * 
 * @author liang
 * @description 队列消息监听器
 * 
 */

@Component
public class QueueReceiverCreditLogListener implements MessageListener {

	private static Logger log = LoggerFactory
			.getLogger(QueueReceiverCreditLogListener.class);

	@Autowired
	private BpmTaskService bpmTaskService;

	@Override
	public void onMessage(Message message) {
			LogUtils.info(log, "comsumer recevies message,%s", message.toString());

			if (message instanceof ObjectMessage) {
				ObjectMessage om = (ObjectMessage) message;
				Object ob = null;
				try {
					ob = om.getObject();
				} catch (JMSException e) {
					LogUtils.error(log, e.getMessage());
				}
				  
				if(ob instanceof ProcessMessageVO){
					
					ProcessMessageVO vo = (ProcessMessageVO)ob;
					
					bpmTaskService.startProcess(vo.getProcDefKey(),vo.getBusinessTable(), vo.getBusinessId(),vo.getTitle(),vo.getLoginName(), vo.getVars());
				}else {
					//TODO:重新扔到产生队列
					throw new BusinessException(String.format("数据类型异常,%s",ob.getClass().toString()));
				}

			}

		
	}

}
